package listerner;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class OnlineListerner implements HttpSessionListener {
    private  int onlineNumber=0;
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        //创建session，在线人数+1
        onlineNumber++;
        httpSessionEvent.getSession().getServletContext().setAttribute("onlineNumber",onlineNumber);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        //销毁session，在线人数-1
        onlineNumber--;
        httpSessionEvent.getSession().getServletContext().setAttribute("onlineNumber",onlineNumber);
    }
}
